package com.guomao.demob;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

public class JavaDemo3 {

    public static void main(String[] args) {

        boolean flag1 = method1("hello world", (String s) -> {
            return s.contains("hello");
        });

//        System.out.println(flag1);

        /*boolean flag2 = method2("hello world", (String s) -> {
            return s.contains("hello");
        });*/

        boolean flag2 = method2("hello world", s -> s.contains("hello"));
//        System.out.println(flag2);

        boolean flag3 = method3("hello world", s -> s.contains("hellos"), s -> s.length() > 5);
        boolean flag4 = method4("hello world", s -> s.contains("hellos"), s -> s.length() > 5);
//        System.out.println(flag3);
//        System.out.println(flag4);

        String[] arr = {"特朗普,60", "蔡徐坤,25", "雨姐,35", "张三,18"};

        List<String> list = method5(arr,
                s -> s.split(",")[0].length() > 2,
                s -> Integer.parseInt(s.split(",")[1]) > 20);


        method5(arr, (String s) -> {
            String[] arr1 = s.split(",");
            String name = arr1[0];
            return name.length() > 2;
        }, (String s) -> {
            String[] arr1 = s.split(",");
            String age = arr1[1];
            Integer num = Integer.parseInt(age);
            return num > 20;
        });
        System.out.println(list);
    }

    public static boolean method1(String s, Predicate<String> p) {
        return p.test(s);
    }

    public static boolean method2(String s, Predicate<String> p) {
        return p.negate().test(s);
    }

    public static boolean method3(String s, Predicate<String> p1, Predicate<String> p2) {

        return p1.and(p2).test(s);
    }

    public static boolean method4(String s, Predicate<String> p1, Predicate<String> p2) {

        return p1.or(p2).test(s);
    }

    public static List<String> method5(String[] names, Predicate<String> p1, Predicate<String> p2) {
        List<String> list = new ArrayList<>();
        for (String name : names) {
            boolean test = p1.and(p2).test(name);
            if (test) list.add(name);
        }
        return list;
    }
}
